package com.swift.violent.mainstay.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.swift.violent.mainstay.config.annotation.EnableRySecurityFunctionConfig;
import com.thebeastshop.forest.springboot.annotation.ForestScan;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;


/**
 * The type String config.
 *
 * @author xuahifeng
 */
@Configuration
@EnableRySecurityFunctionConfig
@ForestScan(basePackages = "com.swift.violent.mainstay")
public class CoreConfig {

    /**
     * 文件上传配置
     *
     * @return factory multipart config element
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        final MultipartConfigFactory factory = new MultipartConfigFactory();
        // 文件最大
        factory.setMaxFileSize(DataSize.ofGigabytes(10L));
        // 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofGigabytes(100L));
        return factory.createMultipartConfig();
    }

    /**
     * 配置spring boot消息解析器
     * Fast json http message converters http message converters.
     *
     * @return the http message converters
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        final FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        final FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastConverter);
    }

}
